<template>
  <div>
    <h3>这个时图书展示页</h3>
    <table border="1">
      <tr>
        <th>图书名称</th>
        <th>图书价格</th>
        <th>图书库存</th>
        <th>图书操作</th>
      </tr>
      <tr v-for="item in list">
        <td>{{ item.name }}</td>
        <td>{{ item.price }}</td>
        <td>{{ item.count }}</td>
        <td>
          <button type="submit" @click="submit(item.id)">点击购买</button>
        </td>
      </tr>
    </table>
  </div>
</template>

<script>
    export default {
        data(){
            return{
                list:[]
            }
        },
        mounted() {
            this.axios.get('http://127.0.0.1:8000/booklist').then(res=>{
                if (res.data.code == 200){
                    this.list = res.data.data
                }
            })
        },
        methods:{
            submit(id){
                this.axios.get('http://127.0.0.1:8000/goumai',{params:{
                      'username':localStorage.getItem('username'),
                        'id':id
                    }}).then(res=>{
                        if (localStorage.getItem('username') != null){
                            if (res.data.code == 200){
                            alert(res.data.data)
                            }else {
                                alert(res.data.data)
                            }
                        }else {
                            this.$router.replace({path:'/login'})
                        }
                })
            }
        }
    }
</script>

<style scoped>

</style>
